<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="utf-8">
    <title>公告管理</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
</head>
<body class="pear-container">

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">公告标题:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="请输入公告标题" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">操作人员:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="createName" placeholder="请输入操作人员" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">类&nbsp;&nbsp;&nbsp;&nbsp;型:</label>
                    <div class="layui-input-inline">
                        <select id="type" name="type">
                            <option value="">公告类型</option>
                            <option value="1">公告</option>
                            <option value="2">通知</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="notice-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

<!--添加和修改弹出层-->
<div style="display: none;padding: 3px" id="saveOrUpdateDiv">
    <form class="layui-form" id="dataFrm" lay-filter="dataFrm" action="">
        <div class="mainBox">
            <div class="main-container">
                <div class="main-container">
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告标题:</label>
                        <div class="layui-input-block">
                            <input type="hidden" name="id">
                            <input type="text" name="title" lay-verify="required" autocomplete="off"
                                   placeholder="请输入公告标题"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告类型:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="type" value="1" title="公告" checked>
                            <input type="radio" name="type" value="2" title="通知">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">状态:</label>
                        <div class="layui-input-block">
                            <input type="radio" name="state" value="1" title="正常" checked>
                            <input type="radio" name="state" value="2" title="停用">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">公告内容:</label>
                        <div class="layui-input-block">
                            <textarea name="content" placeholder="请输入公告内容" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                        lay-filter="notice-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
</div>

<div style="display: none;padding: 3px" id="see">
    <div style="font-size: 18px;">
        发布人:<span id="view_createName"></span>
        <hr>
        发布时间:<span id="view_createTime"></span>
    </div>
    <hr>
    <div id="view_content" style="font-size: 18px;"></div>
</div>

<!--数据表格-->
<div class="layui-card">
    <div class="layui-card-body">
        <table id="notice-table" lay-filter="notice-table"></table>
    </div>
</div>

<!--表头-->
<script type="text/html" id="notice-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add" shiro:hasPermission="Notice:create">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove" shiro:hasPermission="Notice:bathDelete">
        <i class="layui-icon layui-icon-delete"></i>
        批量删除
    </button>
</script>

<!--行工具栏-->
<script type="text/html" id="notice-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" shiro:hasPermission="Notice:update"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove" shiro:hasPermission="Notice:delete"><i class="layui-icon layui-icon-delete"></i>
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="see"><i class="layui-icon layui-icon-read"></i>
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'common'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;

        let cols = [
            [{
                type: 'checkbox'
            },
                {
                    title: '公告ID',
                    field: 'id',
                    align: 'center',
                },
                {
                    title: '公告标题',
                    field: 'title',
                    align: 'center'
                },
                {
                    title: '公告类型',
                    field: 'type',
                    align: 'center',
                    templet: function (d) {
                        return d.type == 1 ? '<font color=green>公告</font>' : '<font color=blue>通知</font>';
                    }
                },
                {
                    title: '状态',
                    field: 'state',
                    align: 'center',
                    templet: function (d) {
                        return d.state == 1 ? '<font color=green>正常</font>' : '<font color=red>停用</font>';
                    }
                },
                {
                    title: '创建者',
                    field: 'createName',
                    align: 'center',
                },
                {
                    title: '创建时间',
                    field: 'createTime',
                    align: 'center',
                },
                {
                    title: '操作',
                    toolbar: '#notice-bar',
                    align: 'center',
                    width: 150
                }
            ]
        ]

        var tableIns = table.render({
            elem: '#notice-table',
            url: '/notice/loadAllNoticePage',
            page: true,
            cols: cols,
            page: true,
            skin: 'line',
            toolbar: '#notice-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print'],
            response: {
                statusCode: 200
            },
            done: function (res, curr, count) {
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            },
        });

        //行监听
        table.on('tool(notice-table)', function (obj) {
            if (obj.event === 'remove') {
                remove(obj);
            } else if (obj.event === 'edit') {
                edit(obj);
            } else if (obj.event === 'see') {
                see(obj);
            }
        });

        //表头监听
        table.on('toolbar(notice-table)', function (obj) {
            if (obj.event === 'add') {
                add();
            } else if (obj.event === 'batchRemove') {
                batchRemove(obj);
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        //表格监听
        form.on('submit(notice-query)', function (data) {
            table.reload('notice-table', {
                where: data.field
            })
            return false;
        });


        //添加
        var mainIndex;
        var url;

        function add() {
            mainIndex = layer.open({
                type: 1,
                title: '添加公告信息',
                shade: 0.1,
                area: ['550px', '420px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/notice/addNotice";
                    $("#dataFrm")[0].reset();
                }
            });
        }


        //修改
        function edit(obj) {
            mainIndex = layer.open({
                type: 1,
                title: '修改公告信息',
                shade: 0.1,
                area: ['550px', '420px'],
                content: $("#saveOrUpdateDiv"),
                success: function (index) {
                    url = "/notice/updateNotice"
                    $("#dataFrm")[0].reset();
                    form.val("dataFrm", obj.data);
                }
            });
        }


        //监听提交按钮
        form.on('submit(notice-save)', function (data) {
            $.ajax({
                url: url,
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            layer.close(mainIndex);//关闭当前页
                            tableIns.reload();
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //删除
        function remove(obj) {
            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/notice/deleteNotice/" + obj.data['id'],
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        //批量删除
        function batchRemove(obj) {

            var ids = common.checkField(obj, 'id');

            if (ids === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要删除?', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/notice/bathDeleteNotice/" + ids,
                    dataType: 'json',
                    type: 'DELETE',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            });
                            tableIns.reload();
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        function see(obj) {
            layer.open({
                type: 1,
                title: [''+obj.data.title+'', 'font-size:18px;text-align: center;'],
                shade: 0.1,
                area: ['630px', '470px'],
                content: $("#see"),
                success: function (index) {
                    $("#view_createName").html("   "+obj.data.createName);
                    $("#view_createTime").html("   "+obj.data.createTime);
                    $("#view_content").html(obj.data.content);
                }
            });
        }

        window.refresh = function (param) {
            table.reload('notice-table');
        }

    })
</script>
</body>
</html>
